<?php
declare(strict_types=1);

namespace App\Services\Tools\AdapaySdk;

class Kernel
{
    private  $config;

    /**
     * @param Config $config
     */
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    public function getApiKeyLive(): string
    {
        return $this->config->apiKeyLive;
    }

    public function getAppId(): string
    {
        return $this->config->appId;
    }

    public function getNotifyUrl(): string
    {
        return $this->config->notifyUrl;
    }

    public function sign(string $url, string $data): string
    {
        $key = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($this->config->rsaPriateKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
        openssl_sign($url . $data, $singature, $key);
        return base64_encode($singature);
    }

    public function verifySign($paramsStr = '', $sign = ''): bool
    {
        $key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($this->config->apiPublicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
        return (bool)openssl_verify($paramsStr, base64_decode($sign), $key);
    }


}